//
//  CTActivitorView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/24.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTActivitorView.h"
#import "CTDebugManager.h"
#import "CTHeader.h"
#import "CTAppLanguage.h"


@interface CTActivitorView ()
{
    UIActivityIndicatorView * acView;
    UIView * shadowView;
    
    UIImageView * gifImageView;
    UIButton * loadingFailedBtn;
}

@end

@implementation CTActivitorView
@synthesize delegate;

- (void)dealloc
{
    [gifImageView removeFromSuperview];
    gifImageView = nil;
    [acView removeFromSuperview];
    acView = nil;
    [CTDebugManager showDebugLogInfo:@"dealloc : CTActivitorView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.frame = CGRectMake(0, 64, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT-64-64);
        self.backgroundColor = [UIColor clearColor];
        
    }
    return self;
}


//传统加载
-(void)showActivitorViewOn:(UIView *)view
{
    
    self.userInteractionEnabled = YES;
    
    
    shadowView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width/2.0-40,self.frame.size.height/2.0-40, 80, 80)];
    shadowView.backgroundColor = [UIColor blackColor];
    shadowView.alpha = 0.7;
    shadowView.layer.cornerRadius = 5;
    [self addSubview:shadowView];
    
    acView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    acView.frame = CGRectMake(self.bounds.size.width/2.0,self.bounds.size.height/2.0, 0, 0);
    [self addSubview:acView];
    
    
    [acView startAnimating];
    [view addSubview:self];
    
}

-(void)showActivitorViewOn:(UIView *)view withType:(CTActivitorViewType)type
{
    
    if (type == CTActivitorViewDefault) {
        [self showActivitorViewOn:view];
    }else{
        self.frame = CGRectMake(0, 64, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT-64);
        self.userInteractionEnabled = YES;
        
        
        shadowView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width/2.0-40,self.frame.size.height/2.0-40-32, 80, 80)];
        shadowView.backgroundColor = [UIColor blackColor];
        shadowView.alpha = 0.7;
        shadowView.layer.cornerRadius = 5;
        [self addSubview:shadowView];
        
        acView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        acView.frame = CGRectMake(self.bounds.size.width/2.0,self.bounds.size.height/2.0-32, 0, 0);
        [self addSubview:acView];
        
        
        [acView startAnimating];
        [view addSubview:self];
    }
}

-(void)removeActivitorView
{
    [acView stopAnimating];
    [self removeFromSuperview];
}




//加载
-(void)showMoeMarkActivitorViewOn:(UIView *)view
{
    gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width/2.0-45/2.0, self.frame.size.height/2.0-24-25, 45, 48)];
    NSArray * gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"loading_two"],[UIImage imageNamed:@"loading_one"],[UIImage imageNamed:@"loading_three"], nil];
    gifImageView.animationImages = gifArray;
    gifImageView.animationDuration = 0.5;
    gifImageView.animationRepeatCount = -1;
    [self addSubview:gifImageView];
    [gifImageView startAnimating];
    
    
//    UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width/2.0-45/2.0, self.frame.size.height/2.0+51-25, 45, 10)];
//    titleLabel.backgroundColor = [UIColor clearColor];
//    titleLabel.textColor = [UIColor colorWithRed:164/255.0 green:164/255.0 blue:164/255.0 alpha:1.0];
//    titleLabel.textAlignment = NSTextAlignmentCenter;
//    titleLabel.text = [CTAppLanguage localizedStringForKey:@"activitor_a" withValue:nil withTable:@"Language"];
//    titleLabel.font = [UIFont systemFontOfSize:16];
//    [self addSubview:titleLabel];
    
    [view addSubview:self];
}
-(void)removeMoeMarkActivitorView
{
    [gifImageView removeFromSuperview];
    [self removeFromSuperview];
}



//网挂了、加载失败
-(void)showLoadingFailedActivitorViewOn:(UIView *)view
                               withText:(NSString *)text
{
   
    loadingFailedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loadingFailedBtn.frame = CGRectMake(self.frame.size.width/2.0-50.5, self.frame.size.height/2.0-49-25, 101, 98);
    [loadingFailedBtn setBackgroundImage:[UIImage imageNamed:@"oneFailed"] forState:UIControlStateNormal];
    [loadingFailedBtn addTarget:self action:@selector(loadingFailedAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:loadingFailedBtn];
    

    UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width/2.0-50.5, self.frame.size.height/2.0+35, 101, 10)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor colorWithRed:164/255.0 green:164/255.0 blue:164/255.0 alpha:1.0];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = text;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:titleLabel];
    
    [view addSubview:self];
}

-(void)removeLoadingFailedActivitorView
{
    self.delegate = nil;
    [loadingFailedBtn removeFromSuperview];
    loadingFailedBtn = nil;
    [self removeFromSuperview];
}

-(void)loadingFailedAction:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(activitorViewLoadingFailed:)]) {
        [self.delegate activitorViewLoadingFailed:self];
    }
}


@end
